﻿using Microsoft.Extensions.DependencyInjection;
using PetaPoco;
using PmSoft;
using PmSoft.ApplicationContext;
using PmSoft.DBContext;
using PmSoft.Repositories;
using PmSoft.Utilities;


namespace Modules.Logs;

/// <summary>
/// 各应用log4net信息日志仓储
/// </summary>
[UseDI(ServiceLifetime.Scoped)]
public class Log4netInfoRepository : Repository<Log4netInfo, LogDbContext>
{
    public Log4netInfoRepository(LogDbContext dbContext, IApplicationContext applicationContext) : base(dbContext, applicationContext)
    {
    }

    /// <summary>
/// 
/// </summary>
/// <param name="query"></param>
/// <param name="pageIndex"></param>
/// <param name="pageSize"></param>
/// <returns></returns>
public IPagedList<Log4netInfo> GetLog4netInfos(Log4netQuery query, int pageIndex, int pageSize)
{
    Sql sql = Sql.Builder;
    sql.Select("id")
        .From("log4net_info");
        //这里根据自己的查询参数判断 
        if (!string.IsNullOrEmpty(query.Keywords))
            sql.Where("(app_name Like @0 or logger like @0 or message like @0) ", "%" + StringUtility.StripSQLInjection(query.Keywords) + "%");
        if (query.StartTime.HasValue)
            sql.Where("date>=@0", query.StartTime.Value);
        sql.OrderBy("id DESC");
        return GetPagingEntities(pageSize, pageIndex, sql);
}
}